perf: bypass ConcreteInstr validation in concrete_instructions#201
Merged
MatthieuDartiailh merged 1 commit intoMay 22, 2026
Merged
Conversation
Two changes to the hot encode loop in _ConvertBytecodeToConcrete.concrete_instructions: 1. ConcreteInstr._from_trusted factory ConcreteInstr(instr_name, c_arg, location=location) at the bottom of the loop went through the full __init__ -> _check_arg -> _set chain on every instruction, even though name/opcode/arg/location are all derived from already-validated Instr objects. A new _from_trusted classmethod bypasses _check_arg entirely and replicates only the size computation from _set, using object.__new__ + direct slot assignment — the same pattern as the existing _from_opcode fast path. 2. Direct private slot access instr.location, instr.name, and instr.arg are properties whose bodies are each a single "return self._xxx". Accessing instr._location, instr._name, and instr._arg directly avoids the property descriptor lookup on every iteration. This is safe because instr is a validated Instr instance at this point (guarded by the assert isinstance check above). Profile data | Hotspot | Before | After | |---|---|---| | concrete_instructions own | 8.07% | 5.44% | | concrete_instructions total | 11.71% | 9.44% | | ConcreteInstr.__init__ (child) | 1.30% total | gone | Throughput (Bytecode.from_code().to_code() on dis module, 30 runs each) | | Avg | Stddev | |---|---|---| | Before | 223.7 r/s | ±4.7 | | After | 246.1 r/s | ±3.3 | +22.4 r/s / +10.0% throughput improvement.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #201 +/- ##
==========================================
+ Coverage 95.40% 95.43% +0.03%
==========================================
Files 7 7
Lines 2110 2126 +16
Branches 456 458 +2
==========================================
+ Hits 2013 2029 +16
Misses 54 54
Partials 43 43 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
MatthieuDartiailh
approved these changes
May 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two changes to the hot encode loop in _ConvertBytecodeToConcrete.concrete_instructions:
ConcreteInstr._from_trusted factory
ConcreteInstr(instr_name, c_arg, location=location) at the bottom of the loop went through the full init -> _check_arg -> _set chain on every instruction, even though name/opcode/arg/location are all derived from already-validated Instr objects. A new _from_trusted classmethod bypasses _check_arg entirely and replicates only the size computation from _set, using object.new + direct slot assignment — the same pattern as the existing _from_opcode fast path.
Direct private slot access
instr.location, instr.name, and instr.arg are properties whose bodies are each a single "return self._xxx". Accessing instr._location, instr._name, and instr._arg directly avoids the property descriptor lookup on every iteration. This is safe because instr is a validated Instr instance at this point (guarded by the assert isinstance check above).
Profile data
concrete_instructionsownconcrete_instructionstotalConcreteInstr.__init__(child)Throughput (Bytecode.from_code().to_code() on dis module, 30 runs each)
+22.4 r/s / +10.0% throughput improvement.